var _store;
var _pagesize=20;
var _search='';
var _cname='';
var name='';
var search='';

//Ext.onReady(function(){
function initArticlesList(){

	/**
	 * 根据cid获取栏目名称并显示在工具栏右侧
	*/  	
		 	_cnamestore = new Ext.data.JsonStore({
		 		fields:['cname'],
		 		url: Ext.get('basePath').getValue()+ 'tjsinfo/tjsoa/articles/Manage.do?method=getRoleName&cid='+_cid
		 	});
		    _cnamestore.on('load', function() {    
				  _cname=_cnamestore.getAt(0).get('cname');
				  name = encodeURI(encodeURI(_cname));
	              _toolbar.addText('<a style="font-weight:bold; color:black;line-height: 22px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'+ _cname + '</a>&nbsp;&nbsp;&nbsp;'); 
				 });
			_cnamestore.load();   		
	/**
	 * 表格各列定义
	 */
	  	var _columnModle = new Ext.grid.ColumnModel({
	 		columns:[
	 		{
	 			header: "标题", menuDisabled: false,dataIndex:'title',sortable:true,renderer:function(s,m,rec){
	 				var str = "<a href=\"" + Ext.get('basePath').getValue()+ "tjsinfo/tjsoa/articles/List.do?method=viewArticle&uuid="+rec.data.uuid +"\" >"+rec.data.title+"</a>";
	 				return str;
	 			}
	 		},{
	 			header: "发布日期", menuDisabled: false,dataIndex:'createDate',width:30
 			},{
	 			header: "排序号", menuDisabled: false,dataIndex:'showOrder',width:20
 			},
 			{
	 			header: "发布人", menuDisabled: false,dataIndex:'userName',width:30
	 		},
	 		{
	 			header: "操作", menuDisabled: false,dataIndex:'handler',width:35,renderer:function(s,m,rec){
	 				var str = "<a href=\"" +Ext.get('basePath').getValue()+ "tjsinfo/tjsoa/articles/Manage.do?method=toUpdateArticles&articles_uuid="+rec.data.uuid+"&cname="+name+"&cid="+_cid+"\">编辑</a>";
	 				if (_type!="no"){
	 					str+="&nbsp;&nbsp;<a href=\"javaScript:deleteArticles('"+rec.data.id +"') \">删除</a>";
	 				}
	 				str+= "&nbsp;&nbsp;<a href=\"" + Ext.get('basePath').getValue()+ "tjsinfo/tjsoa/articles/List.do?method=viewArticle&uuid="+rec.data.uuid +"\" >预览</a>";
	 				return str;
	 			}
	 		}]
	 	});

	/**
	 * 获取表格中显示的内容
	 */	
		_store = new Ext.data.JsonStore({
	 		fields:['id','uuid','title','createDate','userName','handler','showOrder'],
	 		url: Ext.get('basePath').getValue()+ 'tjsinfo/tjsoa/articles/Manage.do?method=getArticlesList&cid=' + _cid,
	 		root: 'aricleslist',
            totalProperty: 'totalCount'
	 	});
/**   
 * 翻页工具栏
 */		
	 var bbar = new Ext.PagingToolbar({
		pageSize:20,
		store:_store,

		//显示右下角信息
		   displayInfo:true,
		   displayMsg:'当前记录 {0} -- {1} 条 共 {2} 条记录',
	       prevText:"上一页",
		   nextText:"下一页",
		   refreshText:"刷新",
		   lastText:"最后页",
		   firstText:"第一页",
		   beforePageText:"当前页",
		   afterPageText:"共{0}页",
		   emptyMsg:'没有记录',
		   items:[
                '-','每页显示行数:',
                 {  name:'txtpagesize',
                	id:'txtpagesize',
                	xtype:'numberfield',
					emptyText:_pagesize,
					width:30          
                },{
                	text: '确定',
			            handler: function(){
			                bbar.pageSize = parseInt(Ext.getCmp('txtpagesize').getValue());
					        _store.reload({params:{start:0,limit:bbar.pageSize}}); 
			                }
            }]
	});
	
	 	var _toolbar;
	
	 	 _toolbar = new Ext.Toolbar({
	         items: [
	           {
	               id: 'buttonA',
				   iconCls: 'add',
	               text: "新建",
	               handler: function() { window.location.href= Ext.get('basePath').getValue()+'tjsinfo/tjsoa/articles/Manage.do?method=toAddArticles&cid='+_cid+"&cname="+name}
				},{
	               id: 'buttonA',
				   iconCls: 'add',
	               text: "设置",
	               handler: function() { window.location.href= Ext.get('basePath').getValue()+'tjsinfo/tjsoa/articles/Manage.do?method=toUpdateView&cid='+_cid+"&cname="+name}
				},'->',
				{   name:'txtsearch',
                	id:'txtsearch',
                	xtype:'textfield',
					width:120          
                },{
                	text: '检索',
                	iconCls:'search',
			            handler: function(){
						    _search = Ext.getCmp('txtsearch').getValue();
						    search = encodeURI(encodeURI(_search));
						    if(_search==null || _search.length==0){
						           alert('请填写检索条件');
						           return;
						    }
						    _store.removeAll();
			                _store.proxy = new Ext.data.HttpProxy({url:encodeURI(Ext.get('basePath').getValue()+ 'tjsinfo/tjsoa/articles/Manage.do?method=getArticlesList&cid=' + _cid + '&search='+search)});
			              	_store.reload({params:{start:0,limit:bbar.pageSize}}); 
			                bbar.updateInfo();
			                }
			   },{
			   }]
			});

				
		var _table = new Ext.grid.GridPanel({
			colModel:_columnModle,
			sm: new Ext.grid.RowSelectionModel({singleSelect:true}),
	 		store:_store,
		    width: Ext.get("showRegional").getWidth(),
		    height:Ext.get("showRegional").getHeight(),
//		    title: _cname,
		    renderTo:Ext.get('showRegional'),
		    frame: true,
		    viewConfig:{
		    	forceFit: true
		    },
		    clicksToEdit:1, 
		    layout : 'fit',// 表格内列内容填充满,按列头比例填充 
		    loadMask:{ 
			msg : '数据加载中，请稍候...' 
			}, 
		    stripeRows:true,
		    tbar: _toolbar,
		    bbar:bbar
		});
		_store.load({params:{start:0, limit:20}});
}
//});

/**
 * 删除
 */
function deleteArticles(uuid){
	Ext.MessageBox.confirm(Ext.systemTitle, '是否删除此文章?', function(btn){
		if(btn=='yes'){
			Ext.Ajax.request({
				url:Ext.get('basePath').getValue()+'tjsinfo/tjsoa/articles/Manage.do?method=deleteArticles',
			    params: { articles_uuid: uuid },
			    success:function(){
			    	Ext.MessageBox.show({ title:Ext.systemTitle,
					   msg: '删除成功！',
					   buttons: Ext.Msg.OK,						  					
					   icon: Ext.MessageBox.INFO
					});
			    	 _store.reload();
			    },
			    failure:function(){
			    	Ext.MessageBox.show({ title:Ext.systemTitle,
					   msg: '删除失败！',
					   buttons: Ext.Msg.OK,						  					
					   icon: Ext.MessageBox.INFO
					});
			    }
			});	
		}
	});
}




 function windowonload(){
         if (_sessionuser==null){
    	    Ext.MessageBox.alert('提示', '请先登录',clickLink);     
         }
        }
        
function clickLink(){
		window.location.href='';
	}		
		